﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using trangbaocao.Models;
using System.Collections;


namespace trangbaocao.Controllers
{
    public class Move_DeviceController : Controller
    {
        //
        // GET: /Move_Device/

        Device_StationsModel Device_StationsModel = new Device_StationsModel();
        StationsModel StationsModel = new StationsModel();
        DeviceModel DeviceModel = new DeviceModel();
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        public ActionResult Index()
        {
            Device_StationsAssociateModel Device_StationsAssociateModel = new Device_StationsAssociateModel();
            List<Device_StationsAssociateModel> Device_Stations = Device_StationsAssociateModel.getDevice_StationsIndex();

            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            return View(Device_Stations);
        }
        public ActionResult FindDevice_Stations()
        {
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult FindMove_Device(string timkiem)
        {
            string trungtam_id = "";
            string tram_id = "";
            int num;
            bool main_stations_id_check = int.TryParse(trungtam_id, out num);
            string s = " d.id is not null AND ";
            if (int.TryParse(Request.Form["main_stations_id"], out num))
            {
                trungtam_id = Request.Form["main_stations_id"];
                s = " d.id=" + trungtam_id ;
            }
            if (int.TryParse(Request.Form["stations_id"], out num))
            {
                tram_id = Request.Form["stations_id"];
                s = " b.id=" + tram_id ;
            }
            if (int.TryParse(Request.Form["main_stations_id"], out num) && int.TryParse(Request.Form["stations_id"], out num))
                s = " d.id=" + trungtam_id + " AND b.id=" + tram_id ;
            if (Session != null && s.ToString() == "")
            {
                int quyen_id = Convert.ToInt16(Session["quyen_id"]);
                int trungtam_session_id = Convert.ToInt16(Session["trungtam_id"]);
                if (quyen_id > 2)
                    s = " d.id=" + trungtam_session_id ;
            }

           

            ViewBag.find_textbox = timkiem;
            ViewBag.find_selectbox = Request["select_option"];
            Device_StationsAssociateModel Device_StationsAssociateModel = new Device_StationsAssociateModel();
            List<Device_StationsAssociateModel> Device_Stations = Device_StationsAssociateModel.FindDevice_Stations(s);

            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");

            return View("Index", Device_Stations);
        }
        public ActionResult Dieuchuyen_Device(int id)
        {
            Device_StationsModel Device_StationsModel_ = Device_StationsModel.getDevice_Stations().Find(delegate(Device_StationsModel s) { return s.id == id; });

            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            Device_StationsModel_.trungtam_id = new SelectList(main_StationsModel, "id", "name");

            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Device_StationsModel_.main_stations_id);
            Device_StationsModel_.tram_id = new SelectList(stationModel, "id", "name");

            List<DeviceModel> deviceModel = DeviceModel.getDevice();
            Device_StationsModel_.thietbi_id = new SelectList(deviceModel, "id", "name");
            return View(Device_StationsModel_);
        }
        [HttpPost]
        public ActionResult Dieuchuyen_Device (Device_StationsModel Device_Stations)
        {
            Move_Device move_dv = new Move_Device();
            Device_StationsModel Device_StationsModel_ = Device_StationsModel.getDevice_Stations().Find(delegate(Device_StationsModel s) { return s.id == Device_Stations.id; });
            move_dv.device_stations_id = Device_Stations.id;
            move_dv.stations_id_from = Device_StationsModel_.stations_id;
            move_dv.stations_id_to = Device_Stations.stations_id;
            move_dv.date_move = DateTime.Now.ToString("dd/MM/yyyy");
            move_dv.note_move = Device_Stations.ghi_chu;
            move_dv.nv_dichchuyen = Device_Stations.nv_lap_dat;
            move_dv.ma_ht_old = Device_StationsModel_.ma_he_thong;
            move_dv.ma_ht_new = Device_Stations.ma_he_thong;
            move_dv.name_tb_old = Device_StationsModel_.name;
            move_dv.name_tb_new = Device_Stations.name;

            Device_StationsModel Device_Stations_ = Device_StationsModel_;
            

            Device_Stations_.ma_he_thong = Device_Stations.ma_he_thong;
            Device_Stations_.name = Device_Stations.name;
            Device_Stations_.main_stations_id = Device_Stations.main_stations_id;
            Device_Stations_.stations_id = Device_Stations.stations_id;
            Device_Stations_.ngay_lap_dat = Device_Stations_.ngay_lap_dat;
            Device_Stations_.ngay_su_dung = Device_Stations_.ngay_su_dung;
            Device_Stations_.trang_thai_hd = Device_Stations.trang_thai_hd;
            Device_Stations_.nv_lap_dat = Device_Stations.nv_lap_dat;
            Device_Stations_.ghi_chu = Device_Stations.ghi_chu;

            

            if (ModelState.IsValid)
            {
                Device_Stations.trang_thai_hd = Convert.ToInt16(Request["trang_thai_hd"]);
                Device_StationsModel.move_thietbi(move_dv);
                Device_StationsModel.updateDevice_Stations(Device_Stations_);
                //add by namtd 07/03/2014
                Device_StationsModel.updateFuel_and_FuelDetail(Device_Stations_);

                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Index");
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";

            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            Device_Stations.trungtam_id = new SelectList(main_StationsModel, "id", "name");

            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Device_Stations.main_stations_id);
            Device_Stations.tram_id = new SelectList(stationModel, "id", "name");

            List<DeviceModel> deviceModel = DeviceModel.getDevice();
            Device_Stations.thietbi_id = new SelectList(deviceModel, "id", "name");
            return View("Index",Device_Stations);
        
        }
        [HttpPost]
        public JsonResult getStationsJson(string MainStationId, string Order)
        {
            return Json(Device_StationsModel.getStations(MainStationId, Order));
        }
        public ActionResult Lichsu_Device(int device_statios_id)
        {
            List<Move_Device> listMoveDevice = Device_StationsModel.getListLichsu(device_statios_id);
            return View(listMoveDevice);
        }
        public ActionResult Device_Configuration_Report_Index()
        {
            return View("~/Views/Move_Device/Report/Device_Configuration_Report_Index.cshtml");
        }
    }
}
